package com.gitee.ywj1352.算法.week02;

public class 计算x的n次幂函数 {


    //分治算法
    public static double myPow(double x, int n) {
        double v = doMyPow(x, n);
        return v;
    }

    public static double doMyPow(double x, int n) {
        //1. 跳出条件

        if (n == 0) {
            return 1.0;
        }
        //2. process (分割问题)
        //3. 递归
        double v = doMyPow(x, n / 2);
        //4. merge
        if (n % 2 == 1) {
            return v * v * 2;
        } else {
            return v * v;
        }

    }

    public static void main(String[] args) {
        System.out.println(myPow(2.0, 10));

    }


}
